﻿using BLL;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Student
{
    public partial class select_teacher : Form
    {
        List<Teacher> teachers;
        Models.g_data m_g = new Models.g_data();
        public select_teacher()
        {
            InitializeComponent();
        }

        private void teacher_list_MouseClick(object sender, MouseEventArgs e)
        {
            int idx = teacher_list.CurrentCell.RowIndex; // 获取当前单元格的行索引
            int choice; // 定义一个变量来存储选择的选项
            System.Windows.Forms.TextBox selected = selected_box(out choice); // 调用selected_box方法来获取选中的文本框
            selected.Text = teachers[idx].teacher_name; // 设置选中文本框的文本为选中教师的名称
            teacher_name.Text = teachers[idx].teacher_name; // 设置teacher_name文本框的文本为选中教师的名称
            teacher_info.Text = teachers[idx].info; // 设置teacher_info文本框的文本为选中教师的信息

            if (choice == 1)
                m_g.c1 = teachers[idx].teacher_id; // 如果选择的是第一选项，设置m_g的c1属性
            else if (choice == 2)
                m_g.c2 = teachers[idx].teacher_id; // 如果选择的是第二选项，设置m_g的c2属性
            else
                m_g.c3 = teachers[idx].teacher_id; // 否则，设置m_g的c3属性
        }

        private System.Windows.Forms.TextBox selected_box(out int choice)
        {
            System.Windows.Forms.TextBox selected; // 定义一个文本框变量

            if (radio_c1.Checked) // 如果radio_c1被选中
            { selected = choice1; choice = 1; } // 设置selected为choice1，choice为1
            else if (radio_c2.Checked) // 如果radio_c2被选中
            { selected = choice2; choice = 2; } // 设置selected为choice2，choice为2
            else // 否则
            { selected = choice3; choice = 3; } // 设置selected为choice3，choice为3

            return selected; // 返回选中的文本框
        }


        private void select_teacher_Load(object sender, EventArgs e)
        {
            B_Teacher b_Teacher = new B_Teacher(); // 创建B_Teacher实例
            teachers = b_Teacher.GetList(); // 调用GetList方法获取教师列表
            teacher_list.DataSource = teachers; // 设置teacher_list的数据源为教师列表

            teacher_list.Columns["teacher_id"].Visible = false;
            teacher_list.Columns["teacher_uname"].Visible = false;
            teacher_list.Columns["pwd"].Visible = false;

            teacher_list.Columns["teacher_name"].HeaderText = "教师姓名";
            teacher_list.Columns["depart"].HeaderText = "单位";
            teacher_list.Columns["info"].HeaderText = "简介";


            B_G b_G = new B_G(); // 创建B_G实例
            m_g = b_G.select(log_info.student); // 调用select方法获取g_data对象

            if (m_g.c1 != null)
                choice1.Text = teachers.Where(t => t.teacher_id == m_g.c1).First().teacher_name; // 如果m_g的c1属性不为空，设置choice1文本框的文本
            if (m_g.c2 != null)
                choice2.Text = teachers.Where(t => t.teacher_id == m_g.c2).First().teacher_name; // 如果m_g的c2属性不为空，设置choice2文本框的文本
            if (m_g.c3 != null)
                choice3.Text = teachers.Where(t => t.teacher_id == m_g.c3).First().teacher_name; // 如果m_g的c3属性不为空，设置choice3文本框的文本
        }


        //private void 提交_Click(object sender, EventArgs e)
        //{
        //    if (log_info.Student_timeout)
        //    {
        //        MessageBox.Show("已经超时，不能再修改！");
        //        return;
        //    }

        //    if(checkDuplicate())
        //    {
        //        MessageBox.Show("有重复选项，请检查重选");
        //        return;
        //    }

        //    m_g.g_id = (int)log_info.student.group_id;
        //    BLL.B_G b_G = new BLL.B_G();


        //    if (b_G.commit(m_g))
        //        MessageBox.Show("ok!");

        //}

        private bool checkDuplicate()
        {
            return new[] { m_g.c1, m_g.c2, m_g.c3 }.GroupBy(x => x).Any(g => g.Count() > 1);

            //int a = m_g.c1;
            //int b = m_g.c2;
            //int c = m_g.c3;

            //if (a == b || b == c || c == a)
            //{
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}

        }

        private void teacher_list_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void radio_c3_CheckedChanged(object sender, EventArgs e)
        {

        }

        //private void button1_Click(object sender, EventArgs e)
        //{
        //    // 获取当前选中的单选按钮和对应的文本框
        //    int choice;
        //    System.Windows.Forms.TextBox selected = selected_box(out choice);
        //    selected.Text = ""; // 清除文本框的内容

        //    // 根据选择更新m_g对象的相应属性为null
        //    if (choice == 1)
        //        m_g.c1 = null;
        //    else if (choice == 2)
        //        m_g.c2 = null;
        //    else if (choice == 3)
        //        m_g.c3 = null;
        //}

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void uiTextBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            // 获取当前选中的单选按钮和对应的文本框
            int choice;
            System.Windows.Forms.TextBox selected = selected_box(out choice);
            selected.Text = ""; // 清除文本框的内容

            // 根据选择更新m_g对象的相应属性为null
            if (choice == 1)
                m_g.c1 = null;
            else if (choice == 2)
                m_g.c2 = null;
            else if (choice == 3)
                m_g.c3 = null;
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (log_info.Student_timeout)
            {
                MessageBox.Show("已经超时，不能再修改！");
                return;
            }

            if (checkDuplicate())
            {
                MessageBox.Show("有重复选项，请检查重选");
                return;
            }

            m_g.g_id = (int)log_info.student.group_id;
            BLL.B_G b_G = new BLL.B_G();


            if (b_G.commit(m_g))
                MessageBox.Show("ok!");
        }

        private void sTreturn1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void choice1_TextChanged(object sender, EventArgs e)
        {

        }

        private void choice2_TextChanged(object sender, EventArgs e)
        {

        }

        private void radio_c1_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
